#import "CustomTabBar.h"
 
@interface CustomTabBar ()
 
@property (strong, nonatomic) NSMutableArray *tabBarItems;
 
@end
 
@implementation CustomTabBar
 
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if (self) {
        [self config];
    }
    
    return self;
}
 
#pragma mark - Private Method
 
- (void)config {
    self.backgroundColor =skUIColorFromRGB(0x4e555f);
    
//    UIImageView *topLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, -5, [UIScreen mainScreen].bounds.size.width, 5)];
//    topLine.image = [UIImage imageNamed:@"tapbar_top_line"];
//    [self addSubview:topLine];
}
 
- (void)setTabBarItemAttributes:(NSArray<NSDictionary *> *)tabBarItemAttributes {
    _tabBarItemAttributes = tabBarItemAttributes.copy;
    
    CGFloat itemWidth = ([UIScreen mainScreen].bounds.size.width) / _tabBarItemAttributes.count;
    CGFloat tabBarHeight = CGRectGetHeight(self.frame);
    NSInteger itemTag = 0;
    BOOL passedRiseItem = NO;
    
    _tabBarItems = [NSMutableArray arrayWithCapacity:_tabBarItemAttributes.count];
    for (id item in _tabBarItemAttributes) {
        if ([item isKindOfClass:[NSDictionary class]]) {
            NSDictionary *itemDict = (NSDictionary *)item;
            CustomTabBarItemType type = [itemDict[CustomTabBarItemAttributeType] integerValue];
            CGRect frame = CGRectMake(itemTag * itemWidth + (passedRiseItem ? itemWidth : 0), 0, itemWidth, tabBarHeight);
            CustomTabBarItem *tabBarItem = [self tabBarItemWithFrame:frame
                                                               title:itemDict[CustomTabBarItemAttributeTitle]
                                                     normalImageName:itemDict[CustomTabBarItemAttributeNormalImageName]
                                                   selectedImageName:itemDict[CustomTabBarItemAttributeSelectedImageName] tabBarItemType:type];
            if (itemTag == 0) {
                tabBarItem.selected = YES;
            }
            [tabBarItem addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchUpInside];
            if (tabBarItem.tabBarItemType != CustomTabBarItemRise) {
                [tabBarItem setTitleColor:skUIColorFromRGB(0xdbdedd) forState:UIControlStateNormal];
                [tabBarItem setTitleColor:skphUIColorFromRGB(0x44cd2c,0.8) forState:UIControlStateSelected];
                tabBarItem.tag = itemTag;
                itemTag++;
            } else {
                [tabBarItem setTitleColor:skUIColorFromRGB(0xdbdedd) forState:UIControlStateNormal];
                [tabBarItem setTitleColor:skphUIColorFromRGB(0x44cd2c,0.8) forState:UIControlStateSelected];
                passedRiseItem = YES;
            }
            
            [_tabBarItems addObject:tabBarItem];
            [self addSubview:tabBarItem];
        }
    }
    
}
 
- (void)itemSelected:(CustomTabBarItem *)sender {
    if (sender.tabBarItemType != CustomTabBarItemRise) {
        [self setSelectedIndex:sender.tag];
    } else {
        if (self.delegate) {
            if ([self.delegate respondsToSelector:@selector(tabBarDidSelectedRiseButton)]) {
                [self.delegate tabBarDidSelectedRiseButton];
            }
        }
    }
}
 
- (void)setSelectedIndex:(NSInteger)index {
    for (CustomTabBarItem *item in self.tabBarItems) {
        if (item.tag == index) {
            item.selected = YES;
        } else {
            item.selected = NO;
        }
    }
    
    UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
    UITabBarController *tabBarController = (UITabBarController *)keyWindow.rootViewController;
    if (tabBarController) {
        tabBarController.selectedIndex = index;
    }
}
 
 
- (CustomTabBarItem *)tabBarItemWithFrame:(CGRect)frame title:(NSString *)title normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName tabBarItemType:(CustomTabBarItemType)tabBarItemType {
    CustomTabBarItem *item = [[CustomTabBarItem alloc] initWithFrame:frame];
    [item setTitle:title forState:UIControlStateNormal];
    [item setTitle:title forState:UIControlStateSelected];
    item.titleLabel.font = [UIFont systemFontOfSize:11];
    UIImage *normalImage = [UIImage imageNamed:normalImageName];
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    [item setImage:normalImage forState:UIControlStateNormal];
    [item setImage:selectedImage forState:UIControlStateSelected];
    [item setTitleColor:[UIColor colorWithWhite:51 / 255.0 alpha:1] forState:UIControlStateNormal];
    [item setTitleColor:[UIColor colorWithWhite:51 / 255.0 alpha:1] forState:UIControlStateSelected];
    item.tabBarItemType = tabBarItemType;
    return item;
}
@end
